import { createApp } from 'vue'
import AppComp from './App.vue'

import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'virtual:svg-icons-register'
import pinia from './store/index'
import router from './router'
import registerGlobComp from './components'
import directives from '@/directives/index'

// element默认主题
import 'element-plus/dist/index.css'
// 公共样式，包含自定义暗黑模式，element重置样式
import '@/styles/index.scss'
// 字体样式
import '@/assets/fonts/font.scss'

/** 加载异步路由 */
// import '@/router/initDynamicRouter'

import type { App } from 'vue'
import { createLifecyle, getMicroApp } from 'vite-plugin-legacy-qiankun'
import pkg from '../package.json'

const microApp = getMicroApp(pkg.name)

let app: App

const render = (props: any = {}) => {
  app = createApp(AppComp)

  /** 导入全部Element-icon */
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }

  app.use(ElementPlus, {
    locale: zhCn,
  })

  app.use(registerGlobComp)

  app.use(pinia)

  app.use(router)

  app.use(directives)

  const container = props.container
    ? props.container.querySelector('#app')
    : '#app'

  app.mount(container)
}

if (microApp.__POWERED_BY_QIANKUN__) {
  createLifecyle(pkg.name, {
    mount(props) {
      // const state = props.actions.getGlobalState()
      // console.log(state)
      render(props)
    },
    bootstrap() {
      console.log('storage bootstrap')
    },
    unmount() {
      app.unmount()
    },
  })
} else {
  render()
}
